const axios = require('axios');
const { error } = require('console');
const fs = require('fs');
const cheerio = require('cheerio');

const http2 = require('http2');


async function  loadActivedata() {

        let res1 = JSON.parse(fs.readFileSync("./static/data/activeData.json","utf-8"))
        let arr = []; 
        res1.forEach(item => {
            item.goodslist.forEach((_item)=>{
                arr.push(_item.content.goodsId)
            })
        });
        let  dataArr = [];
        // arr.forEach(async item=>{
        //     let options = {
        //         url : "https://goods.kaola.com/product/getGoodsRecommendInfo.json?goodsId="+item+"&account=163106946045540746%40pvkaola.163.com&recommendType=1&t=1631239420542",
        //         headers:{
        //             "user-agent":" Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36",
        //         }
        //     }
        //     let res = await axios(options);
        //     // console.log(res.data.data);
        //     dataArr.push(res.data.data)
        // })
        for(let i = 0 ; i < arr.length ; i++){
            let options = {
                url : "https://goods.kaola.com/product/getGoodsRecommendInfo.json?goodsId="+arr[i]+"&account=163106946045540746%40pvkaola.163.com&recommendType=1&t=1631239420542",
                headers:{
                    "user-agent":" Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36",
                }
            }
            let res = await axios(options);
            let obj = {};
            obj[arr[i]] = res.data.data
            dataArr.push(obj)
        }
        
        
        
        console.log(dataArr);

        
    
    fs.writeFile("./static/data/ulikeAll.json",JSON.stringify(dataArr),err=>{
        if (err) {
            throw err
        }else{
            console.log("转存成功");
        }
    })
}
loadActivedata();
